package com.ossean.projectmanager;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
 * 将筛选过程写成多线程，这是入口程序
 * 在这里调用筛选线程
 */
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import com.ossean.projectmanager.lasttabledao.OpenSourceProjectDao;
import com.ossean.projectmanager.lasttabledao.RelativeMemoToOpenSourceProjectDao;
import com.ossean.projectmanager.parttabledao.PartProjectDao;
import com.ossean.projectmanager.projectsfilter.ProjectsFilterThread;
@Component
public class ProjectsFilterProcessMain {
	
	@Resource
	private OpenSourceProjectDao lastProjectDao;

	@Resource
	private PartProjectDao partProjectDao;

	@Resource
	private RelativeMemoToOpenSourceProjectDao matchResultDao;
	
	//线程池，开20个线程
	private ExecutorService pool = Executors.newFixedThreadPool(30);
	
	int minId=0 ;
	int maxId=0 ;
	
	Logger logger = Logger.getLogger(this.getClass());
	
	public static void main(String[] args){
		@SuppressWarnings("resource")
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:/applicationContext*.xml");
		ProjectsFilterProcessMain mainClass = applicationContext.getBean(ProjectsFilterProcessMain.class);
		mainClass.start();
	}
	
	
	private void start()
	{
		/**
		 * 实现了对数据的监测，实时监测是否有新的数据到来
		 * 循环查询数据库看是否有可以筛选的数据（即filteration=0）
		 * 当没有需要筛选的数据时，程序睡眠
		 */
		long startTime = System.currentTimeMillis();
//		while(true)
//		{
			// TODO Auto-generated method stub
			minId = lastProjectDao.getMinId(0);
			maxId = lastProjectDao.getMaxId(0);
			logger.info(minId + ":" + maxId);
			
			//获取当前时间，用于记录线程活动
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String date = df.format(new Date());
			startFilter();
			
			/**
			 * 等待进程池中所有进程执行完毕才开始再一次扫描检查是否有新的数据到来
			 * 即程序在某一时刻检查到有需要筛选的数据时就一次性将这些数据筛选完毕
			 * 再进行下一次扫描检查是否有新的数据到来
			 */
			pool.shutdown();
			while(!pool.isTerminated())
			{
				
			}

			logger.info(date + "时刻开始的筛选已经完成");
			try {
				//休眠20秒等待所有操作完成
				Thread.sleep(20000);
			} catch (InterruptedException e) {
				logger.error(e);
			}
			System.gc();  //手动垃圾回收
			
//			try {
//				//本次扫描的结果处理完毕就休眠两个小时
//				Thread.sleep(2*60*60*1000);
//			} catch (InterruptedException e) {
//				logger.error(e);
//			}
		//}
		
	}
	
	
	//进行具体的筛选操作
		private void startFilter() {
			int startId = minId;
			int endId = minId;
			int sumPerThread = (maxId - minId)/30 + 1;//每个线程处理的数据量
			while(true)
			{	
				/**
				 * 每个线程负责startId到endId部分的数据的筛选
				 */
				endId = startId + sumPerThread;
				
				if(endId < maxId)
				{
					//若minId = maxId,说明筛选已完成，暂时没有需要筛选的数据			
					ProjectsFilterThread filterThread = (ProjectsFilterThread)AppContext.appContext.getBean("ProjectsFilterThread");
					filterThread.setBorder(startId,endId);
					pool.execute(filterThread);
				}
				else
				{
					endId = maxId;
					//若minId = maxId,说明给各线程分配数据已完成。		
					ProjectsFilterThread filterThread = (ProjectsFilterThread)AppContext.appContext.getBean("ProjectsFilterThread");
					filterThread.setBorder(startId,endId);
					pool.execute(filterThread);
					break;
				}
				
				startId = endId + 1;
			}
			logger.info("线程分配完毕");
		}
	
}
